package com.drug.controller;

import com.drug.domain.*;
import com.drug.service.ReturnorderService;
import com.drug.util.DateUtils;
import com.drug.util.ExcelUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/ReturnorderController")
public class ReturnorderController {
    @Autowired
    private ReturnorderService returnorderService;

    /**
     * 查询全部的
     * @param combine
     * @return
     */
    @RequestMapping("/findAllReturnGoods")
    private Result findAllReturnGoods(Combine combine){
        return returnorderService.findAllReturnGoods(combine);
    }

    /**
     * 确认退货
     * @param ids
     * @return
     */
    @RequestMapping("/updateState")
    private Result updateState(Integer[] ids){
        return returnorderService.updateState(ids);
    }

    /**
     * 导出退货单
     *
     */
    @RequestMapping(value = "/findAllExport")
    public String export(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String message = "";
        //获取数据
        List<ReturnGoods> list = returnorderService.findAllExport();
        //excel标题
        String[] title = {"退货单编号","退货单名称","下单医院","开始采购时间","结束采购时间","对应采购单名称","对应采购单编号","流水号","通用名","商品名","剂型","规格","单位","转换系数","质量层次"};
        //excel文件名
        String fileName = "Returnorder"+System.currentTimeMillis()+".xls";
        //sheet名
        String sheetName = "退货单信息表";
        String [][] content = new String[list.size()][];
        for (int i = 0; i < list.size(); i++) {
            content[i] = new String[title.length];
            ReturnGoods returnGoods = list.get(i);
            if (returnGoods!=null){
                //退货单名称
                String rgName = returnGoods.getRgName();
                //退货单编号
                String rgNumber = returnGoods.getRgNumber();
                content[i][0] = rgName+"";
                content[i][1] = rgNumber+"";
                //采购单编号
                PurchaseOrder purchaseOrder = returnGoods.getPurchaseOrder();
                if (purchaseOrder!=null){
                    String poNumber = purchaseOrder.getPoNumber();
                    content[i][5] = poNumber+"";
                    //采购单名称
                    String poName = purchaseOrder.getPoName();
                    content[i][6] = poName+"";
                    //开始采购时间
                    Date poSubmissionTime = purchaseOrder.getPoSubmissionTime();
                    String date2String = DateUtils.date2String(poSubmissionTime,"yyyy-MM-dd");
                    content[i][3] = date2String+"";
                    //结束采购时间
                    Date auditTime = purchaseOrder.getPoAuditTime();
                    String date2String1 = DateUtils.date2String(auditTime, "yyyy-MM-dd");
                    content[i][4] = date2String1+"";
                }
                //采购医院
                Hospital hospital = returnGoods.getHospital();
                if (hospital!=null){
                    String hName = hospital.gethName();
                    content[i][2] = hName+"";
                }
                DrugInformation drugInformation = returnGoods.getDrugInformation();
                if (drugInformation!=null){
                    //药品流水号
                    String diNumer = drugInformation.getDiNumer();
                    content[i][7] = diNumer+"";
                    //通用名
                    String diName = drugInformation.getDiName();
                    content[i][8] = diName+"";
                    //剂型
                    String diForm = drugInformation.getDiForm();
                    content[i][10] = diForm+"";
                    //规格
                    String diSpecifications = drugInformation.getDiSpecifications();
                    content[i][11] = diSpecifications+"";
                    //单位
                    String diUnit = drugInformation.getDiUnit();
                    content[i][12] = diUnit+"";
                    //转换系数
                    String diCc = drugInformation.getDiCc();
                    content[i][13] = diCc+"";
                }
                DrugAtegory drugAtegory = returnGoods.getDrugAtegory();
                if (drugAtegory!=null){
                    String daName = drugAtegory.getDaName();
                    //商品名
                    content[i][9] = daName+"";
                }
                //专利
                DrugQualityLevel drugQualityLevel = returnGoods.getDrugQualityLevel();
                if (drugQualityLevel!=null){
                    String dqlName = drugQualityLevel.getDqlName();
                    content[i][14] = dqlName+"";
                }

            }
        }
        //创建HSSFWorkbook
        HSSFWorkbook wb = ExcelUtil.getHSSFWorkbook(sheetName, title, content, null);
        //响应到客户端
        try {
            this.setResponseHeader(response, fileName);
            OutputStream os = response.getOutputStream();
            wb.write(os);
            os.flush();
            os.close();
            message = "SUCCESS";
        } catch (Exception e) {
            e.printStackTrace();
            message = "ERROR";
        }
        return null;
    }

    private void setResponseHeader(HttpServletResponse response, String fileName) {
        try {
            try {
                fileName = new String(fileName.getBytes(),"utf-8");
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            response.setContentType("application/octet-stream;charset=utf-8");
            response.setHeader("Content-Disposition", "attachment;filename="+ fileName);
            response.addHeader("Pargam", "no-cache");
            response.addHeader("Cache-Control", "no-cache");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
